﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Windows;
using System.IO;
using eWallet.Data.Local;

namespace eWallet
{
	/// <summary>
	/// Interaction logic for App.xaml
	/// </summary>
	public partial class App : Application
	{
		
		protected override void OnStartup(StartupEventArgs e)
		{

			// If the data file does not exit, create it under the application data
			// for the current user and store the path to that file in the properties
			// for the current user.
			if (!File.Exists(eWallet.Properties.Settings.Default.DatabasePath))
			{
				string s = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
				string path = string.Format(@"{0}\CodePlex\e-Wallet\LocalData\eWalletDatabase.sdf", Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData));

				// Now we must check whether the generated path exists. If it exists, it 
				// means that the database path stored in the user's settings was wrong
				// or pointed to a file that no longer exists. It might also be that the
				// path stored in the user's settings was not set yet.
				if (!File.Exists(path))
				{
					DirectoryInfo di = new FileInfo(path).Directory;
					if (!di.Exists)
					{
						di.Create();
					}

					eWalletDataSet.CreateEmptyDatabase(path);
				}

				eWallet.Properties.Settings.Default.DatabasePath = path;
				eWallet.Properties.Settings.Default.Save();
			}
			
			base.OnStartup(e);
		}

	}
}
